package ldh.im.business.service;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import ldh.common.PageResult;
import ldh.im.business.dao.CmsCategoryDao;
import ldh.im.business.pojo.CmsCategory;
import ldh.im.business.pojo.Tenant;
import ldh.im.business.pojo.where.CmsCategoryWhere;
import ldh.im.business.pojo.where.TenantWhere;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: ldh
 * @date: 2021-07-04 17:02:35
*/
@Service("cmsCategoryService")
public class CmsCategoryService {

	@Resource
	private CmsCategoryDao cmsCategoryDao;
	
	@Transactional
	public CmsCategory insert(CmsCategory cmsCategory) {
		int n = cmsCategoryDao.insert(cmsCategory);
		if (n != 1) {
			throw new RuntimeException("CmsCategory insert error!");
		}
		return cmsCategory;
	}
	
	@Transactional
	public CmsCategory insertSelective(CmsCategory cmsCategory) {
		int n = cmsCategoryDao.insert(cmsCategory);
		if (n != 1) {
			throw new RuntimeException("CmsCategory insertSelective error!");
		}
		return cmsCategory;
	}

	@Transactional
	public CmsCategory updateById(CmsCategory cmsCategory) {
		int  n = cmsCategoryDao.updateById(cmsCategory);
		if (n != 1) {
			throw new RuntimeException("CmsCategory update error!");
		}
        return cmsCategory;
     }

	@Transactional
	public CmsCategory updateNotNullById(CmsCategory cmsCategory) {
		int  n = cmsCategoryDao.updateNotNullById(cmsCategory);
		if (n != 1) {
			throw new RuntimeException("CmsCategory update error!");
		}
        return cmsCategory;
     }


    @Transactional
    public void deleteById(Long id) {
		int n = cmsCategoryDao.deleteById(id);
		if (n != 1) {
			throw new RuntimeException("CmsCategory delete error!");
		}
    }

	@Transactional(readOnly = true)
	public CmsCategory getById(Long id) {
		CmsCategory cmsCategory = cmsCategoryDao.getById(id);
		return cmsCategory;
    }
    
	@Transactional(readOnly = true)
	public CmsCategory getByTenantIdAndName(Long tenantId, String name) {
		CmsCategory cmsCategory = cmsCategoryDao.getByTenantIdAndName(tenantId, name);
		return cmsCategory;
    }
    
	
	@Transactional(readOnly = true)
	public PageResult<CmsCategory> findByCmsCategoryWhere(CmsCategoryWhere cmsCategoryWhere) {
		long total = cmsCategoryDao.findTotalByCmsCategoryWhere(cmsCategoryWhere);
		List<CmsCategory> imageList = new ArrayList<CmsCategory>();
		if (total > 0) {
			imageList  = cmsCategoryDao.findByCmsCategoryWhere(cmsCategoryWhere);
		}
		
		return new PageResult<CmsCategory>(cmsCategoryWhere, total, imageList);
	}

	@Transactional(readOnly = true)
	public List<CmsCategory> getAll(CmsCategoryWhere cmsCategoryWhere) {
		return cmsCategoryDao.findByCmsCategoryWhere(cmsCategoryWhere);
	}
}
